<template>
  <div class="list-table">
    <div v-show="!menuKeyMark" class="area-header">
      <span class="area-header-title">{{ $t('费用报销') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input v-model="queryParams.actTitle" :placeholder="$t('申请标题')" size="small" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="userName">
        <el-input v-model="queryParams.userName" :placeholder="$t('申请人')" clearable size="small" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item :label="$t('申请时间')">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <el-form-item v-show="menuKeyMark" :label="$t('是否已维护')" prop="whetherToMaintain">
        <dictSelect
          v-model="queryParams.whetherToMaintain"
          :clearable="true"
          dictionary="payment_maintenance"
          :placeholder="$t('请选择')"
        />
      </el-form-item>
      <el-form-item :label="$t('费用说明')" prop="costsThat">
        <el-input
          v-model="queryParams.costsThat"
          :placeholder="$t('费用说明')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('发票号码')" prop="invoiceCode">
        <el-input
          v-model="queryParams.invoiceCode"
          :placeholder="$t('发票号码')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item v-if="!menuKeyMark" :label="$t('费用类型')" prop="typeArray">
        <el-cascader v-model="queryParams.typeArray" :options="getFdSubjectList" clearable>
          <template slot-scope="{ node, data }">
            <span>{{ $t(data.label) }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template>
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row v-if="!menuKeyMark" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:expense:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['oa:expense:export']" type="warning" icon="el-icon-download" size="mini" @click="exportHandle">{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="oaExpenseList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="left" prop="actTitle" :show-overflow-tooltip="true" width="300">
        <template slot-scope="scope">
          <el-link class="link-a" type="primary" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="userName" width="100" show-overflow-tooltip />
      <el-table-column :label="$t('申请时间')" align="center" prop="applyTime" width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('报销总额(元)')" align="center" prop="amountMoney" width="110">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amountMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('费用说明')" show-overflow-tooltip align="center" prop="costsThat" />
      <el-table-column fixed="right" :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope">
          <div v-if="!menuKeyMark">
            <ListBtn :bus-data="scope.row" :apply="scope.row.createBy.id" has-permi="oa:expense" :other-btn="scope.row.actStatus === 'W' && (scope.row.payCompletionTime === null || scope.row.payCompletionTime === undefined || scope.row.payCompletionTime === '') ? false : true">
              <template v-slot:before>
                <el-button
                  v-if="scope.row.actStatus === 'W' && (scope.row.payCompletionTime === null || scope.row.payCompletionTime === undefined || scope.row.payCompletionTime === '')"
                  v-hasPermi="['oa:payment:edit']"
                  size="small"
                  type="text"
                  icon="el-icon-edit-outline"
                  @click="updatePayTimeBtn(scope.row)"
                >{{ $t('维护实际付款时间') }}</el-button>
              </template>
            </ListBtn>
          </div>
          <div v-else>
            <el-button
              v-if="scope.row.actStatus === 'W' && (scope.row.payCompletionTime === null || scope.row.payCompletionTime === undefined || scope.row.payCompletionTime === '')"
              v-hasPermi="['oa:payment:edit']"
              size="small"
              type="text"
              icon="el-icon-edit-outline"
              @click="updatePayTimeBtn(scope.row)"
            >{{ $t('维护实际付款时间') }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div>
      <el-dialog :title="$t('请维护付款时间')" :visible.sync="dialogVisiblePayTime" width="30%" :close-on-click-modal="false" :lock-scroll="false">
        <div v-loading="updateTimeLoading">
          <DatePicker
            v-model="dialogVisiblePayTimeValue"
            :placeholder="$t('维护实际付款时间')"
            style="width: 100%;"
          />
          <div v-if="!updateTimeLoading" style="bottom: 20px;right: 20px;position: absolute;">
            <el-button @click="dialogVisiblePayTime = false">{{ $t("取消") }}</el-button>
            <el-button type="primary" @click="updatePayTimeSuccess">{{ $t("确定") }}</el-button>
          </div>
        </div>
      </el-dialog>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改费用报销对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm">
      <oaExpenseForm v-if="formOpen" ref="oaExpenseForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listOaExpenseByResult,
  delOaExpense,
  updatePayTime,
  listOaExpenseByTypeArray
} from '@/api/oa/expense/oaExpense'
import ProcessStatus from '@/components/Act/ProcessStatus'
import oaExpenseForm from './oaExpenseForm.vue'
import { getFdSubjectList } from '@/api/fd/config/getTypeMain'
export default {
    components: {
      oaExpenseForm,
      ProcessStatus
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 费用报销表格数据
          oaExpenseList: [],
          // 弹出层标题
          title: '费用报销',
          // 是否显示弹出层
          formOpen: false,
          // 日期范围
          dateRange: [],
          // 费用类型
          getFdSubjectList: [],
          // 查询参数
          queryParams: {
              userId: '',
              userName: undefined,
              applyTime: undefined,
              costsThat: undefined,
              whetherToMaintain: undefined,
              typeArray: [],
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 更新付款时间
          dialogVisiblePayTime: false,
          // 选中的时间
          dialogVisiblePayTimeValue: '',
          // 要修改的ID
          dialogVisiblePayTimeId: '',
          // 表单校验
          rules: {
            userId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            applyTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            amountMoney: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
          },
          menuKeyMark: undefined,
        updateTimeLoading: false
      }
  },
  created() {
      this.getList()
      if (!this.menuKeyMark) {
        // 费用类型
        getFdSubjectList('expense').then(response => {
          this.getFdSubjectList = this.changeSubjectList(response.data)
        })
      }
  },
  methods: {
    /** 查询费用报销列表 */
    getList() {
      this.menuKeyMark = this.$route.params.menuKeyMark
      this.loading = true
      if (this.menuKeyMark) {
        // 维护付款结果
        listOaExpenseByResult(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.oaExpenseList = response.rows
          this.total = response.total
          this.loading = false
        })
      } else {
        listOaExpenseByTypeArray(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.oaExpenseList = response.rows
          this.total = response.total
          this.loading = false
        })
      }
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            userId: undefined,
            applyTime: undefined,
            amountMoney: undefined,
            costsThat: undefined,
            actId: undefined,
            actStatus: undefined,
            remark: undefined,
            payCompletionTime: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            oaExpenseList: []
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.dateRange = []
        this.queryParams.typeArray = []
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        console.log(this.$refs)
        this.$refs.oaExpenseForm.selectedInvoice = []
        this.$refs.oaExpenseForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.oaExpenseForm.selectedInvoice = []
        // 调用form页面的 init 方法初始化数据
        this.$refs.oaExpenseForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delOaExpense(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    // 付款时间维护 : 按钮获取数据
    updatePayTimeBtn(row) {
      this.updatePayTime(row)
    },
    // 付款时间维护 : 将数据传递到打开弹窗
    updatePayTime(row) {
      this.dialogVisiblePayTimeValue = ''
      this.dialogVisiblePayTime = true
      this.dialogVisiblePayTimeId = row.id
    },
    // 付款时间维护 : 保存到后台
    updatePayTimeSuccess() {
      try {
        this.updateTimeLoading = true
        updatePayTime(this.dialogVisiblePayTimeId, new Date(this.dialogVisiblePayTimeValue)).then(() => {
          this.msgSuccess(this.$t('维护成功'))
          this.updateTimeLoading = false
          this.dialogVisiblePayTime = false
          this.getList()
        })
      } catch (e) {
        this.updateTimeLoading = false
        this.msgSuccess(this.$t('维护失败'))
      }
    },
    /** 导出按钮操作 */
    exportHandle() {
        this.downLoadExcelPost('/expense/oaExpense/export', this.queryParams)
    },
    changeSubjectList(val) {
      const arr = []
      if (val.length !== 0) {
        val.forEach(item => {
          const obj = {}
          obj.value = item.value
          obj.label = this.$t(item.label)
          if (item.children && item.children.length > 0) {
            obj.children = this.changeSubjectList(item.children)
          }
          arr.push(obj)
        })
      }
      return arr
    }
  }
}
</script>
